package com.flippy.fl.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.flippy.fl.events.*;
	import com.flippy.fl.model.FlippyModelLocator;
	import com.flippy.fl.model.Logger;
	import com.flippy.fl.model.Main;
	import com.flippy.fl.view.ChatBox;

	public class ChatStatusCommand implements ICommand
	{	
		[Bindable]	
		private var model:FlippyModelLocator = FlippyModelLocator.getInstance();		
		private var logger:Logger = model.logger;
		[Bindable]
		private var main:Main = model.main;
		private var aChatEvent:ChatEvent;		
		
		public function ChatStatusCommand()
		{
			
		}

		public function execute(event:CairngormEvent):void
		{
			aChatEvent = event as ChatEvent;
			
			logger.logMessage( "chat status command.. started: " + aChatEvent.enabled, this);												
			
			if (model.main.role == model.main.ROLE_AUTHOR) {			
				// event should be generated by PublicChat button! 
				
				// update model
				logger.logMessage("about to set public chat button to: " + (!aChatEvent.enabled), this);
				main.publicChatEnabled = aChatEvent.enabled;
				
				logger.logMessage("enabled: " + main.publicChatEnabled, this);
				
				// update RSO
				main.stateRSO.data["publicChatEnabled"] = aChatEvent.enabled;
				main.stateRSO.setDirty("publicChatEnabled");
				
			} else {
				// event generated by sync RSO method 
				// disable view
				var cb:ChatBox = main.chatWindows["Public Chat"] as ChatBox;
				
				if (cb != null) {
					cb.enabled = aChatEvent.enabled;
				}
			}
		}
		
	}
}
